package com.controller;

import com.model.Score;
import com.model.Student;
import com.model.dto.ScoreDTO;
import com.service.ScoreService;
import com.utils.PageParam;
import com.utils.Pager;
import com.utils.R;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RequestMapping("score")
@RestController
public class ScoreController {

    @Resource
    private ScoreService scoreService;

    @RequestMapping(value = "/getScorePage")
    public R getScorePage(String clazzCode, String studentName, Integer pageIndex, Integer pageSize) {
        Pager<ScoreDTO> pager = scoreService.getScorePage(clazzCode, studentName, new PageParam(pageIndex, pageSize));
        return R.ok().put("data", pager);
    }

    @RequestMapping(value = "/getUserScore")
    public R getUserScore(Long studentId) {
        List<ScoreDTO> list = scoreService.getUserScores(studentId);
        return R.ok().put("data", list);
    }


    @RequestMapping("/saveScore")
    public R saveScore(Integer id, String studentId, Integer semester, Double midMarks, Double endMarks, String createUser){
        Score score = new Score();
        score.setId(id);
        score.setStudentId(studentId);
        score.setSemester(semester);
        score.setMidMarks(midMarks);
        score.setEndMarks(endMarks);
        score.setCreateUser(createUser);
        if (id != null) {
            scoreService.updateScore(score);
        } else {
            scoreService.saveScore(score);
        }
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delScore")
    public R delScore(String ids){
        String[] strArr = ids.split(",");
        List<Long> idList = new ArrayList<>();
        for (String str : strArr) {
            idList.add(Long.valueOf(str));
        }
        scoreService.delScore(idList);
        return R.ok();
    }
}
